# Copyright 2001-2007 by iNeed,Inc. All Rights Reserved.

"""
use cases:

"""
from config import *
from common import *
import gameEngine
import database
import driver,passenger

__author__  = "Number Huang<huang.bafeng@iNeed.com>"
__status__  = "developing"
__version__ = "0.1108.31.1"


""""
FSM:

            [NONE]--publish-->[WFR_PUBLISH]--timeout/fail-->[ESE_WF_GUI_ERROR]
                                    |
                                    OK
                                    |
                                    V
                              [WF_ORDER]
                                    |
                                    |

                                getNotify
                                    |
                                    V
                              [WF_GUI_CO] --cancel->[NONE]
                                    |
                            ok and Invite Driver
                                    |
                                    V
                              [WFR_CD]--timeout/fail->[ESE_WF_GUI_ERROR]#TODO: it may need a new interaction
                                    |
                                    OK
                                    |
                                    V
                              [ORDER_DONE]
                                    |
                                Taxi reach
                                    |
                                    V
                              [READY_BORDING]
                                    |
                                file transaction instruction
                                    |
                                    V
                              [WFR_BT]---fail->[NONE]
                                    |
                                    OK

                                    V
                              [TRANSATION_GOING]--arrive->[TRANSATION_DONE]-->[NONE]


  driver:[NONE]-->getNotify->[WF_GUI_CA]--cancel/ok-->[NONE]
                    |
                [WF_GUI_CD] --cancel-->[NONE]
                    |
                    OK
                    |
                    V
                [ORDER_DONE]
                    |
                    execute
                    |
                    V
                [GOFOR_PICKUP]
                    |
                Taxi reach
                    |
                    V
                [READY_PICKUP]
                    |
            receive transaction instruction
                    |
                    V
                [WF_GUI_BT]->[NONE]
                    |
                    OK
                    V
                [TRANSATION_GOING]--arrive->[TRANSATION_DONE]-->[NONE]`






"""
"""
EVT_XXX: 
"""

class UIPeer(object):
    """
    UIPeer is the proxy of peer in GUI world, it maintains a FSM to trace the status,and
    transfers the confirm event from peer to the GUI and accepts the event from GUI to peer.
    So, the only API is event as follows.
    UIPeer--postEvent-->GUI
    UIPeer<--recvEvent--GUI
    """
    def __init__(self,owner):
        #the request from peer
        self.requestQueue=[] #(method,info,reporter)
        #the event from UI
      
        #dispatch the confirm to sub-handler
        self.confirmHandlerMap={}
        self.owner = owner
        self.activeService = None
        self.log = owner.log

    def exit(self):
        if self.peer:
            self.peer.exit()
    """
    request from peer to UIPeer
    """

    def handleRequest(self,method,info,extraInfoDict,reporter):
        """
        @type extraInfoDict: currently, the timeout and service are the two default key
        """
        #TODO: check the FSM if the new status is valid
        #say, BT arrives repeatedly when current status is T_GOING
        self.log.debug("UI got request %s"%method)
        

       
        self.requestQueue.append((method,info,extraInfoDict,reporter))
        id = len(self.requestQueue)-1
        self.postEvent(EVT_CONFIRM[0],{"name":EVT_CONFIRM[1],CSTR3_SERVICEID:id,"method":method,"info":info})

    
    
  

    def changeServiceStatusByID(self,id,newStatus):
        """
        change the service status
        """
        service = self.requestQueue[id][2][CSTR2_SERVICE]
        service.updateStatus(newStatus)

    

    def getPeerPos(self,phone,reporter):
        return self.peer.getPeerPos(phone,reporter)
    """
    location
    """
    def getLocation(self):

        return self.owner.getLocation()
    
    """
    proxy function
    """
    def queryServiceByStatus(self,status):
    
        return self.peer.queryServiceByStatus(status)

class UIDriver(UIPeer):
    """
        UI proxy for driver
    """

    def __init__(self,owner):
        self.peer = driver.Driver(self)
        super(UIDriver,self).__init__(owner)
        self.confirmHandlerMap={
                CSTR_CD:self.Handle_CD
                }
    def Handle_CD(self,paramDict):

        pass


class UIPassenger(UIPeer):
    """
        UI proxy for Passenger
    """

    def __init__(self,owner):
        self.peer = passenger.Passenger(self)
        super(UIPassenger,self).__init__(owner)
        self.confirmHandlerMap={
        CSTR_CO:self.Handle_CO
        }
    def Handle_CO(self,paramDict):

        pass

if __name__=="__main__":

    try:
        pass

    except:

        log1.exception("exit\n")
